package com.jiaeda.shiroweb.service.imp;

import com.jiaeda.shiroweb.entity.SysRole;
import com.jiaeda.shiroweb.entity.SysUser;
import com.jiaeda.shiroweb.entity.SysUserRole;
import com.jiaeda.shiroweb.mapper.SysRoleMapper;
import com.jiaeda.shiroweb.service.SysRoleService;
import com.jiaeda.shiroweb.service.SysUserRoleService;
import com.jiaeda.shiroweb.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by chengchaopeng on 2018/6/27.
 */
@Service
public class SysRoleServiceImp implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;


    @Override
    public SysRole findByRoleKey(String roleKey) {
        return sysRoleMapper.findByRoleKey(roleKey);
    }

    @Override
    public List<SysRole> findByLoginAccount(String loginAccount) {

        SysUser sysUser=sysUserService.findByLoginAccount(loginAccount);
        List<SysUserRole> sysUserRoleList=sysUserRoleService.findByUserId(sysUser.getUserId());
        StringBuffer roleIds=new StringBuffer();
        sysUserRoleList.forEach(sysUserRole -> {
            roleIds.append(sysUserRole.getRoleId()).append(";");
        });
        return sysRoleMapper.findByRoleIds(roleIds.toString().split(";"));
    }
}
